package com.cn.gxa.service;

import com.cn.gxa.mapper.UserMapper;
import com.cn.gxa.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resources;
import java.util.List;

@Service
public class UserService {
    private final UserMapper userMapper;
    @Autowired
    public UserService(UserMapper userMapper) {
        System.out.println("--------------------------UserService构造函数");
        this.userMapper = userMapper;
    }

    public List<User> selectUser() {
        return userMapper.selectUser();
    }

    //    @Transactional(rollbackFor = Exception.class)
    public User getUser(String username) {
        return userMapper.getUser(username);
    }

    //    @Transactional(rollbackFor = Exception.class)
    public void regUser(String username, String password) {
        userMapper.regUser(username, password);
    }

    @Transactional(rollbackFor = Exception.class) //事务 :四个特性
    public int changeAuth(Integer idA, Integer idB) {
        //查询权限
        String  aAuth=userMapper.selectAuth(idA);
        String  bAuth=userMapper.selectAuth(idB);
        //修改
        int  aRows=userMapper.updateAuth(idA,bAuth);
        System.out.println("执行结果:"+aRows);
        if(idB==3){
             throw  new NullPointerException();
        }
        int  bRows=userMapper.updateAuth(idB,aAuth);

        return  aRows==1&&bRows==1?1:0;
        //  opeSession(false)//  不自动提交
        //try{
        //查询  A  B  auth
        //change  updateA  updateB
        //}  (Exception){   session.rolback  //回滚}
        //session.commmit

    }
}